﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assignment_3.Classes;

namespace Assignment_3.Feature___Users
{
    public partial class UpdateUser : System.Web.UI.Page
    {
        string username = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null || Session["usertype"] == null)
            {
                Response.Redirect("~/Feature - Users/Login.aspx");
            }

            if (((string)Session["usertype"]) != "Administrator")
            {
                Response.Redirect("~/Other - Error Pages/NoPermission.aspx");
            }

            if (Request.QueryString["ID"] != null)
            {
                username = Request.QueryString["ID"];

                if (!IsPostBack)
                {
                    UserAccount user = DatabaseManager.GetUserAccountByUsername(username);
                    Staff s = DatabaseManager.GetStaffByUsername(username);

                    tbxUsername.Text = user.Username.ToString();
                    ddlUserType.SelectedValue = user.UserType.ToString();

                    tbxStaffID.Text = s.StaffID.ToString();
                    tbxName.Text = s.Name.ToString();
                    CalendarExtender1.SelectedDate = s.DOB;
                    tbxBankNo.Text = s.BankNo.ToString();
                    tbxAddress.Text = s.Address.ToString();
                    tbxTelNo.Text = s.TelNo.ToString();
                    ddlDutyType.SelectedValue = s.DutyType.ToString();
                }
            }

            else
            {
                Response.Redirect("~/Feature - Users/RetrieveUser.aspx");
            }
        }

        protected void updateBtn_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                UserAccount user = new UserAccount();
                Staff s = new Staff();
                int rowsUpdated = 0;

                user.Username = tbxUsername.Text;
                user.UserType = ddlUserType.SelectedItem.Text;
                user.Password = tbxConfirmPassword.Text;

                s.Name = tbxName.Text;
                s.DOB = Convert.ToDateTime(tbxDOB.Text);
                s.BankNo = Convert.ToInt32(tbxBankNo.Text);
                s.Address = tbxAddress.Text;
                s.TelNo = Convert.ToInt32(tbxTelNo.Text);
                s.DutyType = ddlDutyType.SelectedItem.Text;
                s.Username = tbxUsername.Text;

                rowsUpdated = DatabaseManager.UpdateUserAccount(user);

                if (rowsUpdated != 0)
                {
                    rowsUpdated = 0;

                    rowsUpdated = DatabaseManager.UpdateStaff(s);
                    string ID = tbxUsername.Text.ToString();
                    string query = "ID=" + ID;

                    string update = "true";
                    query += "&UPDATE=" + update;

                    Response.Redirect("~/Feature - Users/RetrieveUser.aspx?" + query);
                }
            }
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            UserAccount user = DatabaseManager.GetUserAccountByUsername(username);
            args.IsValid = (((string)user.Password) == tbxCurrentPassword.Text);
        }

        protected void tbxCurrentPassword_TextChanged(object sender, EventArgs e)
        {
            CustomValidator1.Validate();
        }
    }
}